//
//  LSPageView.m
//  LookScore
//
//  Created by wws on 2018/3/21.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSPageView.h"
#import "LSRightCell.h"
#import "LSExponentModel.h"
@interface LSPageView ()<UITableViewDelegate,UITableViewDataSource>{
    BOOL _isRelate;
}

@property (nonatomic,strong) UITableView *leftTableView;
@property (nonatomic,strong) UITableView *rightTableView;
@property (nonatomic,strong) NSArray *data;
//@property (nonatomic,assign) NSInteger selectIndex;

@end

@implementation LSPageView{
    
    companyData * tmpModel;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
        [self getData];
    }
    
    return self;
}


#pragma mark - gate
- (void)getData{
    self.data = @[
                  @{@"title" : @"00",
                    @"list" : @[@"Soldier", @"aaa0", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"01",
                    @"list" : @[@"Soldier", @"aaa1", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"02",
                    @"list" : @[@"Soldier", @"aaa2", @"aaa"]
                    },
                  @{@"title" : @"03",
                    @"list" : @[@"Soldier", @"aaa3", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"04",
                    @"list" : @[@"Soldier", @"aaa4", @"aaa"]
                    },
                  @{@"title" : @"05",
                    @"list" : @[@"Soldier", @"aaa5", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"06",
                    @"list" : @[@"Soldier", @"aaa6", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"07",
                    @"list": @[@"Soldier", @"aaa7", @"aaa"]
                    },
                  @{@"title": @"08",
                    @"list": @[@"Soldier" ,@"aaa8", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"09",
                    @"list" : @[@"Soldier", @"aaa9", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"10",
                    @"list" : @[@"Soldier", @"aaa10", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"11",
                    @"list" : @[@"Soldier", @"aaa11", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"12",
                    @"list" : @[@"Soldier", @"aaa12", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"13",
                    @"list" : @[@"Soldier", @"aaa13", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"14",
                    @"list" : @[@"Soldier", @"aaa14", @"aaa", @"aaa", @"aaa1", @"aaa", @"aaa", @"aaa", @"aaa"]
                    },
                  @{@"title" : @"15",
                    @"list" : @[@"Soldier", @"aaa15", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa1", @"aaa"]
                    },
                  @{@"title" : @"16",
                    @"list" : @[@"Soldier", @"aaa16", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa", @"aaa"]
                    }
                  ];
    [self.leftTableView reloadData];
    [self.rightTableView reloadData];
}

#pragma mark - ui
- (void)createUI{
    [self addSubview:self.leftTableView];
    [self addSubview:self.rightTableView];
    
//    self.
}



- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    
    [self.leftTableView reloadData];
    [self.rightTableView reloadData];
    
    NSIndexPath * selectIndexPath = [NSIndexPath indexPathForRow:_selectIndex inSection:0];

    [self.leftTableView selectRowAtIndexPath:selectIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self tableView:self.leftTableView didSelectRowAtIndexPath:selectIndexPath];
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.leftTableView) {
        return 1;
    } else {
        //        return self.data.count;
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSDictionary *item = [self.data objectAtIndex:_selectIndex];
    
    
    
    if (tableView == self.leftTableView) {
        
        return self.dataArray.count;
        
    } else {
        if (self.dataArray.count == 0) {
            return 0;
        }
        AIHistoricaDataList * model = self.dataArray[_selectIndex];
        return model.companyData.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (tableView == self.leftTableView) {
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            cell.layoutMargins = UIEdgeInsetsZero;
        }
        cell.contentView.backgroundColor = LSBackGroundColor;
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        selectedBackgroundView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = selectedBackgroundView;
        cell.textLabel.font = LSFontSize12;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
         AIHistoricaDataList * model = self.dataArray[indexPath.row];
        cell.textLabel.text = model.CompanyName;
        return cell;
    } else {
     LSRightCell * cell1 =  [LSRightCell RightTableViewCellBytableView:tableView];
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            cell1.layoutMargins = UIEdgeInsetsZero;
        }
        cell1.type = self.selectType;
        if (self.selectType == 0) {
            OddsChangeRecordList * model = self.dataArray[_selectIndex];
            cell1.model = model.companyData[indexPath.row];
            if (indexPath.row == model.companyData.count-1) {
                tmpModel = model.companyData[indexPath.row];
            }else{
                tmpModel = model.companyData[indexPath.row+1];
            }
            cell1.tmpModel = tmpModel;
        }else if (self.selectType == 1){
            
            AIHistoricaDataList * model = self.dataArray[_selectIndex];
            
            cell1.model = model.companyData[indexPath.row];
            if (indexPath.row == model.companyData.count-1) {
                tmpModel = model.companyData[indexPath.row];
            }else{
                tmpModel = model.companyData[indexPath.row+1];
            }
            cell1.tmpModel = tmpModel;
            
        }else{
            SDHistoricaDataList * model = self.dataArray[_selectIndex];
            cell1.model = model.companyData[indexPath.row];
            if (indexPath.row == model.companyData.count-1) {
                tmpModel = model.companyData[indexPath.row];
            }else{
                tmpModel = model.companyData[indexPath.row+1];
            }
            cell1.tmpModel = tmpModel;
        }
        
//        NSDictionary *item = [self.data objectAtIndex:_selectIndex];
        
        return cell1;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        return 30;
    } else {
        return 30;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (tableView == self.leftTableView) {
        return 0;
    } else {
        return CGFLOAT_MIN;
    }
}


//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    if (tableView == self.rightTableView) {
//        return [[self.data objectAtIndex:section] objectForKey:@"title"];
//    }else{
//        return nil;
//    }
//}

//- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
//    if (_isRelate) {
//        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section];
//        if (tableView == self.rightTableView) {
//            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        }
//    }
//}
//
//- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section {
//    if (_isRelate) {
//        NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section];
//        if (tableView == self.rightTableView) {
//            [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:topCellSection inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        }
//    }
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        _isRelate = NO;
//        [self.leftTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
        _selectIndex = indexPath.row;
        [self.rightTableView reloadData];
        //        [self.rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    } else {
        [self.rightTableView deselectRowAtIndexPath:indexPath animated:NO];
        
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    _isRelate = YES;
}


#pragma mark - getter
- (UITableView *)leftTableView{
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width/5, self.frame.size.height) style:UITableViewStylePlain];
        _leftTableView.separatorInset = UIEdgeInsetsZero;
        _leftTableView.backgroundColor = [UIColor whiteColor];
        _leftTableView.dataSource = self;
        _leftTableView.delegate = self;
        _leftTableView.tableFooterView = [UIView new];
        
    }
    return _leftTableView;
}

- (UITableView *)rightTableView{
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.frame.size.width/5, 0, self.frame.size.width * 4 / 5 , self.frame.size.height) style:UITableViewStylePlain];
        _rightTableView.separatorInset = UIEdgeInsetsZero;
        _rightTableView.backgroundColor = [UIColor whiteColor];
        _rightTableView.tableFooterView = [UIView new];
        _rightTableView.dataSource = self;
        _rightTableView.delegate = self;
    }
    return _rightTableView;
}
@end
